﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.Reports;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.ExpenseClaim;
using SCMS.UI.Models;
using SCMS.Model;

namespace SCMS.UI.Areas.Reports.Controllers
{
    [MyException]
    public class EClaimController : PortalBaseController
    {
        private IExpenseClaimService ECService;
        public EClaimController(IPermissionService permissionService, IExpenseClaimService ECService, IUserContext uc)
            : base(uc, permissionService)
        {
            this.ECService = ECService;
        }
        //
        // GET: /Reports/EClaim/

        public ActionResult pdf(Guid ECid)
        {
            Dictionary<String, String> data = new Dictionary<string, string>();
            ViewExpsClaims Model = ExpsClaimExtension.PrepareECFModel(ECService, ECid);
            using (var db = new SCMSEntities())
            {
                data.Add("{REQ_NO.}", Model.EntityExpseRqst.RefNumber);
                data.Add("{CLAIM-DATE}", Model.EntityExpseRqst.RequestDate.ToShortDateString());
                data.Add("{EC-CURRENCY}", Model.ExpsCurrency.ShortName);
                data.Add("{CURRENCY-CONVERTED2}", Model.Converted2Currency.ShortName);
                data.Add("{CLAIM-BY}", Model.ClaimBy.StaffName);
                data.Add("{CAIM-TITLE}", Model.ClaimBy.Designation);
                data.Add("{EXG-RATE}", Model.EntityExpseRqst.ExchangeRate.ToString("##,###"));

                int count = 1;
                String detailHtml = "";
                foreach (var item in Model.ExpsItems)
                {
                    count++;
                    detailHtml += "<tr class=\"gridodd\"><td style=\"width: 20%; text-align: left;\">";
                    detailHtml += item.ItemDescription;
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += "";
                    detailHtml += "</td><td style=\"width: 12%; text-align: left;\">";
                    detailHtml += item.ProjectBudget.BudgetCategory.ProjectDonor.ProjectNumber;
                    detailHtml += "</td><td style=\"width: 12%; text-align: left;\">";
                    detailHtml += item.ProjectBudget.LineNumber;
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += Model.ExpsCurrency.ShortName;
                    detailHtml += "</td><td style=\"width: 12%; text-align: right;\">";
                    detailHtml += item.ItemAmount.ToString("##,##0.00");
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += Model.EntityExpseRqst.ExchangeRate.ToString("##,##0.00");
                    detailHtml += "</td><td style=\"width: 12%; text-align: right;\">";
                    detailHtml += item.ConvertedAmount.HasValue ? item.ConvertedAmount.Value.ToString("##,##0.00") : "";
                    detailHtml += "</td></tr>";

                }
                while (count < 14)
                {
                    count++;
                    detailHtml += "<tr class=\"gridodd\"><td style=\"width: 20%; text-align: left;\">&nbsp;&nbsp;";
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += "</td><td style=\"width: 12%; text-align: left;\">";
                    detailHtml += "</td><td style=\"width: 12%; text-align: left;\">";
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += "</td><td style=\"width: 12%; text-align: right;\">";
                    detailHtml += "</td><td style=\"width: 10%; text-align: left;\">";
                    detailHtml += "</td><td style=\"width: 12%; text-align: right;\">";
                    detailHtml += "</td></tr>";
                }
                data.Add("{DETAILS}", detailHtml);

                data.Add("{MB-CURRENCY}", "");//db.Currencies.FirstOrDefault(p => p.Id == Model.MBCurrencyId).ShortName);
                data.Add("{MB-TOTAL}", Model.MBValue.ToString("##,##0.00"));

                String authourisation = "";
                authourisation = AuthName(Model, authourisation);
                authourisation = AuthTitle(Model, authourisation);
                authourisation = AuthDate(Model, authourisation);
                authourisation = AuthSignature(Model, authourisation);
                data.Add("{AUTHORISATION}", authourisation);

                String ECDetailsHTML = "";
                ECDetailsHTML = ECName(Model, ECDetailsHTML);
                ECDetailsHTML = ECDate(Model, ECDetailsHTML);
                ECDetailsHTML = ECSignature(Model, ECDetailsHTML);
                data.Add("{EC-DETAILS}", ECDetailsHTML);

            }
            List<String> options = new List<string>();
            options.Add(" --copies 2 ");
            Byte[] output = WkHtml2Pdf.CreateReport(data, "Expense-Claim.htm", options);

            return File(output, "application/pdf", "EC_" + DateTime.Now.FormatDDMMMYYYYHHmm());
        }

        private static String AuthName(ViewExpsClaims Model, String authourisation)
        {

            authourisation += "<tr><th>Name:</th><td>";
            authourisation += Model.PreparedBy.StaffName;
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null)
                authourisation += Model.ApprovedBy.StaffName;
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null)
                authourisation += Model.AuthorizedBy.StaffName;
            authourisation += "</td><tr>";

            return authourisation;
        }

        private static String AuthTitle(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th>Title:</th><td>";
            authourisation += Model.PreparedBy.Designation;
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null)
                authourisation += Model.ApprovedBy.Designation;
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null)
                authourisation += Model.AuthorizedBy.Designation;
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthDate(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th>Date:</th><td>";
            authourisation += Model.EntityExpseRqst.PreparedOn.Value.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null)
                authourisation += Model.EntityExpseRqst.ReviewedOn.Value.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null)
                authourisation += Model.EntityExpseRqst.AuthorizedOn.Value.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String AuthSignature(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th>Signature:</th><td>";
            if (Model.PreparedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.PreparedBy.SignatureImage));
            authourisation += "</td><td>";
            if (Model.ApprovedBy != null && Model.ApprovedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.ApprovedBy.SignatureImage));
            authourisation += "</td><td>";
            if (Model.AuthorizedBy != null && Model.AuthorizedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 100px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.AuthorizedBy.SignatureImage));
            authourisation += "</td><tr>";
            return authourisation;
        }

        private static String ECName(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th >Name:</th><td >";
            if (Model.ApprovedBy != null)
                authourisation += Model.ApprovedBy.StaffName;
            authourisation += "</td><td >";
            authourisation += Model.PreparedBy.StaffName;
            authourisation += "</td><td >";
            authourisation += "Adv. Received";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td></tr>";

            return authourisation;
        }

        private static String ECDate(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th >Date:</th><td >";
            if (Model.ApprovedBy != null)
                authourisation += Model.EntityExpseRqst.ReviewedOn.Value.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td >";
            authourisation += Model.EntityExpseRqst.PreparedOn.Value.ToString("dd/MM/yyyy hh:mm tt");
            authourisation += "</td><td >";
            authourisation += "Total Expenses";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td></tr>";
            return authourisation;
        }

        private static String ECSignature(ViewExpsClaims Model, String authourisation)
        {
            authourisation += "<tr><th rowspan=\"2\">Signature:</th><td rowspan=\"2\">";
            if (Model.ApprovedBy != null && Model.ApprovedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 80px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.ApprovedBy.SignatureImage));
            authourisation += "</td><td rowspan=\"2\">";
            if (Model.PreparedBy.SignatureImage.IsNotNull())
                authourisation += string.Format("<img src=\"{0}\" alt=\"\" style=\"max-width: 80px;\" />", "data:image/png;base64," + Convert.ToBase64String(Model.PreparedBy.SignatureImage));
            authourisation += "</td><td >";
            authourisation += "Balance Due";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td></tr>";

            authourisation += "<tr><td>Settlement Paid</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td><td>";
            authourisation += "";
            authourisation += "</td></tr>";
            return authourisation;
        }

    }
}
